package com.sec.android.app.myfiles.dialog;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.operation.FileOperator;

import java.util.ArrayList;

/**
 * Created by sev_user on 2/21/2017.
 */
public class NotEnoughStorageDialog extends AbsDialogFragment {
    private static final String ARGS_TITLE = "args_title";
    private static final String ARGS_NOT_ENOUGH_STORAGE_MSG = "args_not_enough_storage_msg";

    public static NotEnoughStorageDialog createInstance(Context context, ArrayList<FileRecord> src, String msg, FileOperator.Operation op) {
        NotEnoughStorageDialog dialog = new NotEnoughStorageDialog();
        dialog.setArguments(getArgs(context, src, msg, op));

        return dialog;
    }

    private static Bundle getArgs(Context context, ArrayList<FileRecord> src, String msg, FileOperator.Operation op) {
        Bundle args = new Bundle();
        args.putString(ARGS_NOT_ENOUGH_STORAGE_MSG, msg);

        boolean hasFolder = false;
        boolean hasFile = false;
        int srcCount = 0;
        if (src != null) {
            srcCount = src.size();
            for (FileRecord tmp : src) {
                if (tmp.isDirectory()) {
                    hasFolder = true;
                } else {
                    hasFile = true;
                }
            }
        }
        args.putString(ARGS_TITLE, getQuantityTitle(context, op, srcCount, hasFolder, hasFile));

        return args;
    }

    private static String getQuantityTitle(Context context, FileOperator.Operation op, int count, boolean hasFolder, boolean hasFile) {
        Resources res = context.getResources();
        String result = null;
        String itemMsg = null;
        int folderMsgResId = -1;
        int fileMsgResId = -1;

        switch (op) {
            case COPY:
                folderMsgResId = R.plurals.could_not_copy_folder;
                fileMsgResId = R.plurals.could_not_copy_file;
                if (hasFolder && hasFile) {
                    itemMsg = res.getString(R.string.could_not_copy_items);
                }
                break;
            case MOVE:
                folderMsgResId = R.plurals.could_not_move_folder;
                fileMsgResId = R.plurals.could_not_move_file;
                if (hasFolder && hasFile) {
                    itemMsg = res.getString(R.string.could_not_move_items);
                }
                break;
            case RESTORE:
                folderMsgResId = R.plurals.could_not_restore_folder;
                fileMsgResId = R.plurals.could_not_restore_file;
                if (hasFolder && hasFile) {
                    itemMsg = res.getString(R.string.could_not_restore_items);
                }
                break;
            case CREATE_DIR:
                result = res.getString(R.string.could_not_create_folder);
            default:
                break;
        }

        if (hasFolder && hasFile) {
            result = itemMsg;
        } else if (hasFolder) {
            if (folderMsgResId != -1) {
                result = res.getQuantityString(folderMsgResId, count, count);
            }
        } else if (hasFile) {
            if (fileMsgResId != -1) {
                result = res.getQuantityString(fileMsgResId, count, count);
            }
        }
        if (result == null) {
            result = "";
        }

        return result;
    }

    @Override
    protected Dialog _createDialog() {
        Bundle bundle = getArguments();
        String title = bundle.getString(ARGS_TITLE);
        String message = bundle.getString(ARGS_NOT_ENOUGH_STORAGE_MSG);
        if (title == null || message == null) {
            return null;
        }

        final SamsungAnalyticsLog.ScreenPath screenPath = message.equals(getContext().getString(R.string.msg_not_enough_space_cloud)) ?
                SamsungAnalyticsLog.ScreenPath.NOT_ENOUGH_STORAGE_POPUP : SamsungAnalyticsLog.ScreenPath.MAXIMUM_ITEM_REACHED;

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title).setMessage(message)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SamsungAnalyticsLog.sendLog(screenPath, SamsungAnalyticsLog.Event.OK, null);
                        dialog.dismiss();
                    }
                });

        return builder.create();
    }

    @Override
    public void dismissByBroadcast() {
    }
}

